﻿using System;
using System.Data;

namespace Utility
{
    public class ReformDataTable
    {
        public static ReformDataTable Instance
        {
            get
            {
                return new ReformDataTable();
            }
        }

        public DataTable ReformDataTableByAge(DataTable dtbl)
        {
            if (dtbl == null)
            {
                return null;
            }
            if (dtbl.Rows.Count>0 && dtbl.Columns.Contains("BIRTH_DATE"))
            {
                if (!dtbl.Columns.Contains("REFORM_AGE"))
                {
                    dtbl.Columns.Add("REFORM_AGE", typeof(string));
                    dtbl.Columns.Add("CURRENT_REFORM_AGE", typeof(string));
                }

                foreach (DataRow drow in dtbl.Rows)
                {
                    string birthdayString = drow["BIRTH_DATE"].ToString();
                    DateTime now = DateTime.Now;
                    if (dtbl.Columns.Contains("CALC_DATE"))
                    {
                        string calc_date_string = drow["CALC_DATE"].ToString();
                        if (!DateTime.TryParse(calc_date_string, out now))
                        {
                            now = DateTime.Now;
                        }
                    }

                    string calcAgeString = ParseBirthday(birthdayString, now);
                    string ageString = ParseBirthday(birthdayString, DateTime.Now);

                    drow["REFORM_AGE"] = calcAgeString;
                    drow["CURRENT_REFORM_AGE"] = ageString;
                }
                return dtbl;
            }
            else
            {
                return dtbl;
            }
        }

        public DataSet ReformDataTableByAge(DataSet dset)
        {
            if (dset == null || dset.Tables.Count == 0)
            {
                return dset;
            }

            DataTable dtbl = dset.Tables[0];
            if (dtbl.Columns.Contains("BIRTH_DATE"))
            {
                if (!dtbl.Columns.Contains("REFORM_AGE"))
                {
                    dtbl.Columns.Add("REFORM_AGE", typeof(string));
                    dtbl.Columns.Add("CURRENT_REFORM_AGE", typeof(string));
                }

                foreach (DataRow drow in dtbl.Rows)
                {
                    string birthdayString = drow["BIRTH_DATE"].ToString();
                    DateTime now = DateTime.Now;
                    if (dtbl.Columns.Contains("CALC_DATE"))
                    {
                        string calc_date_string = drow["CALC_DATE"].ToString();
                        if (!DateTime.TryParse(calc_date_string, out now))
                        {
                            now = DateTime.Now;
                        }
                    }

                    string calcAgeString = ParseBirthday(birthdayString, now);
                    string ageString = ParseBirthday(birthdayString, DateTime.Now);

                    drow["REFORM_AGE"] = calcAgeString;
                    drow["CURRENT_REFORM_AGE"] = ageString;
                }
                dtbl.AcceptChanges();
                DataTable dtbl0 = dtbl.Copy();
                DataSet dsetClone = new DataSet();
                dsetClone.Tables.Add(dtbl0);
                for (int i = 1; i < dset.Tables.Count; i++)
                {
                    dsetClone.Tables.Add(dset.Tables[i].Copy());
                }
                return dsetClone;
            }
            else
            {
                return dset;
            }
        }

        private string ParseBirthday(string birthdayString, DateTime targetDateTime)
        {
            int year = targetDateTime.Year;
            int month = targetDateTime.Month;
            int days = targetDateTime.Day;

            DateTime currentMoment = new DateTime();
            if (!DateTime.TryParse(birthdayString, out currentMoment))
            {
                return "";
            }

            if ((targetDateTime.Year - currentMoment.Year) > 6 )
            {
                return string.Format("{0}岁", targetDateTime.Year - currentMoment.Year);
            }
            

            int currentYear = currentMoment.Year;
            int currentMonth = currentMoment.Month;
            int currentDays = currentMoment.Day;
            //if (currentMoment.isValid()) {
            var diffYear = year - currentYear;
            var diffMonth = month - currentMonth;
            if (diffMonth < 0)
            {
                diffMonth = month + 12 - currentMonth;
                diffYear--;
            }
            var diffDays = days - currentDays;
            if (diffDays < 0)
            {
                DateTime prevMonthDate = currentMoment.AddMonths(-1);
                int step = DateTime.DaysInMonth(prevMonthDate.Year, prevMonthDate.Month); // parseInt(moment(startDayOfMonth).subtract(1, "days").format("DD"))
                diffDays = days + step - currentDays;
                --diffMonth;
                if (diffMonth < 0)
                {
                    diffMonth = diffMonth + 12;
                    diffYear--;
                }
            }
            return string.Format("{0}岁{1}月{2}天", diffYear, diffMonth, diffDays);
        }
    }
}